import Cookies from 'js-cookie'

/**
 * 设置 localStorage 数据
 * @param key 键
 * @param value 值
 */
export const setLocalStorage = (key: string, value: string) => {
  localStorage.setItem(key, value)
}

/**
 * 获取 localStorage 数据
 * @param key 键
 */
export const getLocalStorage = (key: string): string => {
  return localStorage.getItem(key) || ''
}

/**
 * 删除 localStorage 数据
 * @param key 键
 */
export const removeLocalStorage = (key: string) => {
  localStorage.removeItem(key)
}

/**
 * 清空所有 localStorage 数据
 */
export const clearLocalStorage = () => {
  localStorage.clear()
}

/**
 * 设置 sessionStorage 数据
 * @param key 键
 * @param value 值
 */
export const setSessionStorage = (key: string, value: string) => {
  sessionStorage.setItem(key, value)
}

/**
 * 获取 sessionStorage 数据
 * @param key 键
 */
export const getSessionStorage = (key: string): string => {
  return sessionStorage.getItem(key) || ''
}

/**
 * 删除 sessionStorage 数据
 * @param key 键
 */
export const removeSessionStorage = (key: string) => {
  sessionStorage.removeItem(key)
}

/**
 * 清空所有 sessionStorage 数据
 */
export const clearSessionStorage = () => {
  sessionStorage.clear()
}

/**
 * 设置 cookie 数据
 * @param key 键
 * @param value 值
 * @param expires 过期时间 单位：天
 */
export const setCookie = (key: string, value: string, expires = 7) => {
  Cookies.set(key, value, { expires })
}

/**
 * 获取 cookie 数据
 * @param key 键
 */
export const getCookie = (key: string) => {
  return Cookies.get(key) || null
}

/**
 * 删除 cookie 数据
 * @param key 键
 */
export const removeCookie = (key: string) => {
  Cookies.remove(key)
}

/**
 * 清空所有 cookie 数据
 */
export const clearCookies = () => {
  for (const key in Cookies.get()) {
    Cookies.remove(key)
  }
}
